package com.xuecheng.publish.listener;
import com.xuecheng.common.util.JsonUtil;
import com.xuecheng.publish.entity.CoursePubMsg;
import com.xuecheng.publish.service.CoursePubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * <p></p>
 *
 * @Description:
 */
@Slf4j
@Component
public class PublishPageConsumer {
    @Autowired
    private CoursePubService coursePubService;
    
    
    /*
    * # 课程发布 交换机名称
        course.publish.exchange = course_pub.direct
        # 课程发布 页面生成队列名称
        course.publish.queue = course_page.queue
        course.publish.routingkey= publish.course
    * */
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue(value = "${course.publish.queue}"),
                    exchange = @Exchange(value = "${course.publish.exchange}"),
                    key = {"${course.publish.routingkey}"}
            )
    })
    public void publishPage(String jsonString) {

        // 1.接受消息并解析为对象
        CoursePubMsg coursePubMsg = JsonUtil.jsonToObject(jsonString, CoursePubMsg.class);
        log.info("课程发布消息生成页面执行：{}",coursePubMsg.getPubId());

        // 2.调用service完成本地事务操作
        coursePubService.publishPage(coursePubMsg);

    }
}